package com.xbongbong.paas.script.controller;

import com.xbongbong.paas.help.menu.InitPriceMenuHelp;
import com.xbongbong.paas.script.distributor.controller.DistributorExplainScriptController;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.script.pojo.dto.ProductDTO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/priceSet")
public class PriceSetScriptController {

    private static final Logger LOG = LoggerFactory.getLogger(DistributorExplainScriptController.class);

    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private InitPriceMenuHelp initPriceMenuHelp;

    @RequestMapping(value = "/addPriceMenu", method = RequestMethod.POST)
    public String addPriceMenu(@RequestBody @Valid ProductDTO productDTO, BindingResult br) throws Exception {
        if (!proBaseConfig.getXbbCode().equals(productDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006);
        }
        long start = DateTimeUtil.getInt();
        MenuInitDTO menuInitDTO = new MenuInitDTO();
        BeanUtil.copyProperties(productDTO, menuInitDTO, true);
        menuInitDTO.setAlias(XbbRefTypeEnum.DISTRIBUTOR.getAlias());
        initPriceMenuHelp.initMenu(menuInitDTO, new HashMap<>());
        LOG.warn("consume:" + (DateTimeUtil.getInt() - start));
        return "success";
    }
}
